package com.mindskip.xzs.service.impl;
//针对任务考试客户答案的操作，包括插入新的任务考试客户答案和更新已存在的任务考试客户答案。
import com.mindskip.xzs.domain.ExamPaper;
import com.mindskip.xzs.domain.ExamPaperAnswer;
import com.mindskip.xzs.domain.TaskExamCustomerAnswer;
import com.mindskip.xzs.domain.TextContent;
import com.mindskip.xzs.domain.task.TaskItemAnswerObject;
import com.mindskip.xzs.repository.TaskExamCustomerAnswerMapper;
import com.mindskip.xzs.service.TaskExamCustomerAnswerService;
import com.mindskip.xzs.service.TextContentService;
import com.mindskip.xzs.utility.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class TaskExamCustomerAnswerImpl extends BaseServiceImpl<TaskExamCustomerAnswer> implements TaskExamCustomerAnswerService {

    // 注入任务考试客户答案的Mapper和文本内容服务
    private final TaskExamCustomerAnswerMapper taskExamCustomerAnswerMapper;
    private final TextContentService textContentService;

    @Autowired
    public TaskExamCustomerAnswerImpl(TaskExamCustomerAnswerMapper taskExamCustomerAnswerMapper, TextContentService textContentService) {
        super(taskExamCustomerAnswerMapper);
        this.taskExamCustomerAnswerMapper = taskExamCustomerAnswerMapper;
        this.textContentService = textContentService;
    }

    // 插入或更新任务考试客户答案的方法
    @Override
    public void insertOrUpdate(ExamPaper examPaper, ExamPaperAnswer examPaperAnswer, Date now) {
        Integer taskId = examPaper.getTaskExamId(); // 获取任务考试ID
        Integer userId = examPaperAnswer.getCreateUser(); // 获取用户ID
        TaskExamCustomerAnswer taskExamCustomerAnswer = taskExamCustomerAnswerMapper.getByTUid(taskId, userId); // 根据任务ID和用户ID查询任务考试客户答案

        if (null == taskExamCustomerAnswer) { // 如果查询结果为空，则表示需要新建
            taskExamCustomerAnswer = new TaskExamCustomerAnswer();
            taskExamCustomerAnswer.setCreateTime(now); // 设置创建时间
            taskExamCustomerAnswer.setCreateUser(userId); // 设置创建用户ID
            taskExamCustomerAnswer.setTaskExamId(taskId); // 设置任务考试ID
            // 创建任务项答案对象的列表
            List<TaskItemAnswerObject> taskItemAnswerObjects = Arrays.asList(new TaskItemAnswerObject(examPaperAnswer.getExamPaperId(), examPaperAnswer.getId(), examPaperAnswer.getStatus()));
            // 将任务项答案对象列表转换为JSON并插入文本内容
            TextContent textContent = textContentService.jsonConvertInsert(taskItemAnswerObjects, now, null);
            textContentService.insertByFilter(textContent); // 插入文本内容
            taskExamCustomerAnswer.setTextContentId(textContent.getId()); // 设置文本内容ID
            insertByFilter(taskExamCustomerAnswer); // 插入任务考试客户答案
        } else { // 如果查询结果不为空，则表示需要更新
            TextContent textContent = textContentService.selectById(taskExamCustomerAnswer.getTextContentId()); // 查询文本内容
            // 将文本内容转换为任务项答案对象的列表
            List<TaskItemAnswerObject> taskItemAnswerObjects = JsonUtil.toJsonListObject(textContent.getContent(), TaskItemAnswerObject.class);
            // 添加新的任务项答案对象
            taskItemAnswerObjects.add(new TaskItemAnswerObject(examPaperAnswer.getExamPaperId(), examPaperAnswer.getId(), examPaperAnswer.getStatus()));
            // 更新文本内容
            textContentService.jsonConvertUpdate(textContent, taskItemAnswerObjects, null);
            textContentService.updateByIdFilter(textContent); // 更新文本内容
        }
    }

    // 根据任务ID和用户ID查询任务考试客户答案的方法
    @Override
    public TaskExamCustomerAnswer selectByTUid(Integer tid, Integer uid) {
        return taskExamCustomerAnswerMapper.getByTUid(tid, uid);
    }

    // 根据任务ID列表和用户ID查询任务考试客户答案列表的方法
    @Override
    public List<TaskExamCustomerAnswer> selectByTUid(List<Integer> taskIds, Integer uid) {
        return taskExamCustomerAnswerMapper.selectByTUid(taskIds, uid);
    }
}
